<?php
namespace common\modules\comment\models;

use common\modules\user\models\User;
use Yii;
use yii\behaviors\TimestampBehavior;
use common\modules\comment\models\query\CommentQuery;
use yii\behaviors\BlameableBehavior;
use yii\behaviors\AttributeBehavior;
use yii\db\ActiveRecord;
use api\modules\v1\resources\UserProfile;
use common\behaviors\UserIpBehavior;

/**
 * This is the model class for table "{{%comment}}".
 *
 * @property integer $id
 * @property integer $parent_id
 * @property string $reply_ids
 * @property integer $user_id
 * @property string $user_nickname
 * @property string $user_ip
 * @property string $user_address
 * @property string $object_type
 * @property integer $object_id
 * @property integer $level
 * @property integer $score
 * @property string $content
 * @property integer $usable_count
 * @property integer $created_at
 *
 * @property Comment $parent
 * @property Comment[] $comments
 * @property User $user
 */
class Comment extends \yii\db\ActiveRecord
{

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%comment}}';
    }

    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            [
                'class' => BlameableBehavior::className(),
                'createdByAttribute' => 'user_id',
                'updatedByAttribute' => false
            ],
            [
                'class' => AttributeBehavior::className(),
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => 'user_nickname',
                    ActiveRecord::EVENT_BEFORE_UPDATE => false
                ],
                'value' => function($event) {
                    $user = Yii::$app->get('user', false);
                    if ($user && !$user->isGuest) {
                        $userProfile = UserProfile::findOne(['user_id' => $user->id]);
                        return $userProfile->nickname;
                    }
                    else {
                        return null;
                    }
                }
            ],
            [
                'class' => UserIpBehavior::className()
            ],
            [
                'class' => TimestampBehavior::className(),
                'updatedAtAttribute' => false
            ]
        ];
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [
                [
                    'object_type',
                    'object_id'
                ],
                'required'
            ],
            [
                [
                    'parent_id',
                    'user_id',
                    'object_id',
                    'level',
                    'score',
                    'usable_count'
                ],
                'integer'
            ],
            [
                [
                    'content'
                ],
                'string'
            ],
            [
                [
                    'user_nickname',
                    'object_type'
                ],
                'string',
                'max' => 20
            ],
            [
                [
                    'reply_ids',
                    'user_ip',
                    'user_address'
                ],
                'string',
                'max' => 128
            ]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('common', 'ID'),
            'parent_id' => Yii::t('common', 'Parent ID'),
            'reply_ids' => Yii::t('common', 'Reply IDs'),
            'user_id' => Yii::t('common', 'User ID'),
            'user_nickname' => Yii::t('common', 'User Nickname'),
            'user_ip' => Yii::t('common', 'User IP'),
            'user_address' => Yii::t('common', 'User Address'),
            'object_type' => Yii::t('common', 'Object Type'),
            'object_id' => Yii::t('common', 'Object ID'),
            'level' => Yii::t('common', 'Level'),
            'score' => Yii::t('common', 'Score'),
            'content' => Yii::t('common', 'Content'),
            'usable_count' => Yii::t('common', 'Usable Count'),
            'created_at' => Yii::t('common', 'Created At')
        ];
    }

    /**
     *
     * @return \yii\db\ActiveQuery
     */
    public function getParent()
    {
        return $this->hasOne(Comment::className(), [
            'id' => 'parent_id'
        ]);
    }

    /**
     *
     * @return \yii\db\ActiveQuery
     */
    public function getComments()
    {
        return $this->hasMany(Comment::className(), [
            'parent_id' => 'id'
        ]);
    }

    /**
     *
     * @return \yii\db\ActiveQuery
     */
    public function getUser()
    {
        return $this->hasOne(User::className(), [
            'id' => 'user_id'
        ]);
    }

    /**
     * @inheritdoc
     *
     * @return \common\modules\comment\models\query\CommentQuery the active query used by this AR class.
     */
    public static function find()
    {
        return new CommentQuery(get_called_class());
    }
}
